﻿Imports System.ServiceModel.DomainServices.Client
Imports LullabyFullDeCaixa.Web

Partial Public Class DetallGestioTipusDespesaIngres
    Inherits ChildWindow

    Public Enum EnumMode
        GestioIngressos
        GestioDespeses
    End Enum

    Private _Mode As EnumMode = EnumMode.GestioDespeses
    Public ReadOnly Property Mode As EnumMode
        Get
            Return _Mode
        End Get
    End Property

#Region "Crear i Destruir"
    Public Sub New(pMode As EnumMode)
        _Mode = pMode

        InitializeComponent()
    End Sub

    Private Sub Child_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, True)
    End Sub

    Private Sub DetallGestioTipusDespesaIngres_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If Mode = EnumMode.GestioDespeses Then
            Me.Title = StringLibrary.GestioTipusDespeses
            Me.listTipusDespesaIngres.ItemTemplate = Me.Resources("TipusDespesaItemTemplate")
        Else
            Me.Title = StringLibrary.GestioTipusIngressos
            Me.listTipusDespesaIngres.ItemTemplate = Me.Resources("TipusIngresItemTemplate")
        End If

        If RiaContext.Current.Authentication.User.Identity.IsAuthenticated = True Then
            Me.Activity.IsActive = True

            AddHandler Contexte.TipusDespesaIngresCarregats, AddressOf TipusDespesaIngresCarregats
            Contexte.LoadTipusDespesaIngres()
        End If
    End Sub
#End Region

#Region "Guardar i Cancelar"
    Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        EliminarObjectErrors()

        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            If ValidarDades() Then
                LullabyFullDeCaixa.Contexte.DomainContext.SubmitChanges(AddressOf DomainContext_SubmittedChanges, Nothing)
                Activity.IsActive = True
            End If
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Function ValidarDades() As Boolean
        Dim Resultat As Boolean = True

        Return Resultat
    End Function

    Private Sub DomainContext_SubmittedChanges(ByVal s As SubmitOperation)
        Activity.IsActive = False

        If s.HasError Then
            If s.Error.Message.Contains("REFERENCE") Then
                Dim ValidationItem As New ValidationSummaryItem()
                ValidationItem.MessageHeader = StringLibrary.ImpossibleEliminar
                If Mode = EnumMode.GestioDespeses Then
                    ValidationItem.Message = StringLibrary.DatosRelacionadosTipusDespesa
                Else
                    ValidationItem.Message = StringLibrary.DatosRelacionadosTipusIngres
                End If
                ValidationItem.ItemType = ValidationSummaryItemType.ObjectError
                Validacio.Errors.Add(ValidationItem)

                s.MarkErrorAsHandled()
            Else
                App.ReportError(StringLibrary.ErrorGuardantDades)
            End If
            Else
                Me.DialogResult = True
            End If
    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            LullabyFullDeCaixa.Contexte.DomainContext.RejectChanges()
        End If

        Me.DialogResult = False
    End Sub

    Public Sub EliminarObjectErrors()
        Dim i As Long

        i = 0
        While i < Validacio.Errors.Count
            If Validacio.Errors(i).ItemType = ValidationSummaryItemType.ObjectError Then
                Validacio.Errors.RemoveAt(i)
            Else
                i = i + 1
            End If
        End While
    End Sub
#End Region

#Region "Carregar Dades"
    Private _TipusDespesaIngresCarregats As Boolean = False

    Public Sub TipusDespesaIngresCarregats(ByVal Resultat As Boolean, ByVal Missatge As String)
        RemoveHandler Contexte.TipusDespesaIngresCarregats, AddressOf TipusDespesaIngresCarregats
        Me.Activity.IsActive = False

        If Mode = EnumMode.GestioDespeses Then
            listTipusDespesaIngres.ItemsSource = LullabyFullDeCaixa.Contexte.DomainContext.TipusDespesaIngres.Where(Function(f) f.EsGasto)
        Else
            listTipusDespesaIngres.ItemsSource = LullabyFullDeCaixa.Contexte.DomainContext.TipusDespesaIngres.Where(Function(f) Not f.EsGasto)
        End If
    End Sub
#End Region

#Region "Boto Eliminar i afegir"

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim TipusDespesaIngres As TipusDespesaIngres = DirectCast(listTipusDespesaIngres.SelectedItem, TipusDespesaIngres)

        LullabyFullDeCaixa.Contexte.DomainContext.TipusDespesaIngres.Remove(TipusDespesaIngres)

        If Mode = EnumMode.GestioDespeses Then
            listTipusDespesaIngres.ItemsSource = LullabyFullDeCaixa.Contexte.DomainContext.TipusDespesaIngres.Where(Function(f) f.EsGasto)
        Else
            listTipusDespesaIngres.ItemsSource = LullabyFullDeCaixa.Contexte.DomainContext.TipusDespesaIngres.Where(Function(f) Not f.EsGasto)
        End If
    End Sub


    Private Sub btnAfegir_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim nouTipusDespesaIngres As LullabyFullDeCaixa.Web.TipusDespesaIngres

        If Mode = EnumMode.GestioDespeses Then
            nouTipusDespesaIngres = New LullabyFullDeCaixa.Web.TipusDespesaIngres() With {.Nom = "Default Tipo Gasto", .EsGasto = True, .EsBaixa = False}
        Else
            nouTipusDespesaIngres = New LullabyFullDeCaixa.Web.TipusDespesaIngres() With {.Nom = "Default Tipo Ingreso", .EsGasto = False, .EsBaixa = False}
        End If

        Contexte.DomainContext.TipusDespesaIngres.Add(nouTipusDespesaIngres)

        If Mode = EnumMode.GestioDespeses Then
            listTipusDespesaIngres.ItemsSource = LullabyFullDeCaixa.Contexte.DomainContext.TipusDespesaIngres.Where(Function(f) f.EsGasto)
        Else
            listTipusDespesaIngres.ItemsSource = LullabyFullDeCaixa.Contexte.DomainContext.TipusDespesaIngres.Where(Function(f) Not f.EsGasto)
        End If

        listTipusDespesaIngres.SelectedItem = nouTipusDespesaIngres

        txtNom.Focus()
        txtNom.SelectAll()
    End Sub

#End Region

    Private Sub listTipusDespesaIngres_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        If e.AddedItems.Count = 0 Then
            Me.gridDetall.Visibility = Windows.Visibility.Collapsed
            Me.btnEliminar.IsEnabled = False
        Else
            Me.gridDetall.Visibility = Windows.Visibility.Visible
            Me.btnEliminar.IsEnabled = True
        End If
    End Sub

    Private Sub Scroll_SizeChanged(ByVal sender As System.Object, ByVal e As System.Windows.SizeChangedEventArgs)
        Me.Components.MaxHeight = Math.Max(Me.Scroll.ViewportHeight, Me.Components.MinHeight)
        Me.Components.MaxWidth = Math.Max(Me.Scroll.ViewportWidth, Me.Components.MinWidth)
    End Sub

End Class

